<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Order;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class OrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(\App\Models\Order::query ()->with ("user.department")->orderBy ("id","desc")->orderBy ("department_id","ASC")->whereDate ('created_at', now ()->toDateString ()), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableEditButton();

            $grid->column('id')->sortable();
            $grid->column('user.name',"用户");
            $grid->column('user.department.name',"部门");
            $grid->column('goods_name');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $titles = ['id' => '序号', 'user.name' => '用户','user.department.name'=>'部门' ,'goods_name' => '商品名称'];
            $grid->export($titles);
            $grid->export()->disableExportSelectedRow();
            $grid->export()->disableExportCurrentPage();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->disableEditButton();
            $show->field('id');
            $show->field('uid');
            $show->field('department_id');
            $show->field('goods_name');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('department_id');
            $form->text('goods_name');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
